﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;
using System.Text.RegularExpressions;

namespace MultiWindows.Forms
{
    public partial class FormVideoSetURL : Form
    {
        public FormVideoSetURL()
        {
            InitializeComponent();
        }

        private void btnConfirm_Click(object sender, EventArgs e)
        {
            if (!this.txtURL.Text.ToLower().StartsWith("http"))
            {
                MessageBox.Show("请输入正确的网址!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            else 
            {
                string strURL = ResolveVideoURL(this.txtURL.Text.Trim());
                if (!strURL.ToLower().StartsWith("http"))
                {
                    MessageBox.Show("请输入正确的网址!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                else 
                {
                    FormMain.VideoURL = strURL;
                }
            }

            MessageBox.Show("保存成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            FormMain.VideoPath = "";
            FormMain.VideoDVD = "";
            ((FormMain)this.MdiParent).SaveSettings();            
            ((FormMain)this.MdiParent).RefreshOpenedVideo();
            this.Close();
        }

        private string ResolveVideoURL(string url)
        {
            string strPage = "";
            string strFid = "";
            string strURL = "";
            bool isCourse = false;

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            WebResponse response = null;
            response = request.GetResponse();
            Stream readStream = response.GetResponseStream();
            StreamReader sr = new StreamReader(readStream, Encoding.Default);
            strPage = sr.ReadToEnd();

            if (strPage.Contains("在线课程"))
            {
                isCourse = true;
            }
            else 
            {
                isCourse = false;
            }

            string strMatch = Regex.Match(strPage, "<param[ ]+name[ ]*=[ ]*\"movie\"[ ]+value[ ]*=[ ]*\"[^\"]*\"").ToString();
            strFid = Regex.Match(strMatch, "fid=[^\"]*\"").ToString();
            strFid = strFid.Substring(strFid.IndexOf('=') + 1, strFid.Length - strFid.IndexOf('=') - 2);

            if (isCourse)
            {
                strURL = "http://media.sse.com.cn/newedu/video/college/" + strFid + ".flv";
            }
            else 
            {
                strURL = "http://media.sse.com.cn/newedu/video/" + strFid + ".flv";
            }
            
            //if (strURL.ToLower().StartsWith("/sseportal"))
            //{
            //    strURL = "http://media.sse.com.cn" + strURL;
            //}

            return strURL;
        }
    }
}
